package com.nisco.ampf.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.nisco.ampf.R;

public class MyListViewAdapter extends BaseAdapter {

	// 数据
	private ArrayList<HashMap<String, Object>> arrayList;
	// checkbox选中时
	public ArrayList<HashMap<String, Object>> selectArrayList;
	// 记录checkbox状态
	public static Map<Integer, Boolean> isSelected;
	private LayoutInflater inflater;
	private Context context;

	public MyListViewAdapter(Context context,
			ArrayList<HashMap<String, Object>> arrayList) {
		this.context = context;
		this.arrayList = arrayList;
		// inflater = (LayoutInflater) context
		// .getSystemService(context.LAYOUT_INFLATER_SERVICE);
		inflater = LayoutInflater.from(context);
		// 定义isSelected这个map是记录每个listitem的状态，初始全为false
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < arrayList.size(); i++) {
			isSelected.put(i, false);
		}
	}


	public int getCount() {
		return arrayList.size();
	}


	public Object getItem(int position) {
		return position;
	}


	public long getItemId(int position) {
		return position;
	}


	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.plate_list_sub, null);
			holder.cBox = (CheckBox) convertView
					.findViewById(R.id.stackCheckBox);
			holder.view1 = (TextView) convertView
					.findViewById(R.id.stackLocTxt);
			holder.view2 = (TextView) convertView
					.findViewById(R.id.stackLayTxt);
			holder.view3 = (TextView) convertView
					.findViewById(R.id.plateNumTxt);
			holder.view4 = (TextView) convertView
					.findViewById(R.id.threadStatTxt);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.view1.setText(arrayList.get(position).get("YARD_ADDR").toString());
		holder.view2.setText(arrayList.get(position).get("BED_SEQ").toString());
		holder.view3.setText(arrayList.get(position).get("PLATE_NO").toString());
		holder.view4.setText(arrayList.get(position).get("PROC_CD").toString());
		holder.cBox.setChecked(isSelected.get(position));
		selectArrayList = new ArrayList<HashMap<String, Object>>();
		holder.cBox.setFocusable(false);
		holder.cBox.setOnClickListener(new OnClickListener() {


			public void onClick(View v) {
				if (isSelected.get(position)) {
					isSelected.put(position, false);
				} else {
					isSelected.put(position, true);
//					Toast.makeText(context, "位置:" + position+1, 1000).show();
				}
				notifyDataSetChanged();
			}
		});
		// 获取被选择的列

		for (int i = 0; i < arrayList.size(); i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			if (isSelected.get(i) == true) {
				map.put("YARD_ADDR", arrayList.get(i).get("YARD_ADDR").toString());
				map.put("PLATE_NO", arrayList.get(i).get("PLATE_NO").toString());
				selectArrayList.add(map);
			}
		}
		return convertView;
	}

	public final class ViewHolder {
		public CheckBox cBox;
		public TextView view1;
		public TextView view2;
		public TextView view3;
		public TextView view4;
	}

}
